/*
 * ModifyAndDeleteProductCategoryFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package tw.com.jinwei.ui;

import java.beans.PropertyVetoException;
import java.util.Iterator;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import org.apache.log4j.Logger;

import tw.com.jinwei.db.Function;
import tw.com.jinwei.db.ProductCategory;
import tw.com.jinwei.db.User;
import tw.com.jinwei.service.Login;
import tw.com.jinwei.service.ProductService;
import tw.com.jinwei.utils.ContextUtils;
import tw.com.jinwei.utils.LogHelper;
import tw.com.jinwei.utils.PermissionConstant;

/**
 * 
 * @author __USER__
 */
public class ModifyAndDeleteProductCategoryFrame extends
		javax.swing.JInternalFrame {

	/** Creates new form ModifyAndDeleteProductCategoryFrame */
	public ModifyAndDeleteProductCategoryFrame() {
		initComponents();
		this.checkPermission();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		jLabel1 = new javax.swing.JLabel();
		jLabel2 = new javax.swing.JLabel();
		jTextField1 = new javax.swing.JTextField();
		jTextField2 = new javax.swing.JTextField();
		jButton1 = new javax.swing.JButton();
		jButton2 = new javax.swing.JButton();

		jLabel1.setText("\u5546\u54c1\u7c21\u78bc");

		jLabel2.setText("\u5546\u54c1\u898f\u683c");

		jTextField1.setColumns(50);

		jTextField2.setColumns(50);

		jButton1.setText("\u66f4\u65b0");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jButton2.setText("\u522a\u9664");
		jButton2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton2ActionPerformed(evt);
			}
		});

		org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout
				.setHorizontalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.LEADING)
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jLabel1)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				jTextField1,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jLabel2)
																		.addPreferredGap(
																				org.jdesktop.layout.LayoutStyle.RELATED)
																		.add(
																				jTextField2,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																				org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																				org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
														.add(
																layout
																		.createSequentialGroup()
																		.add(
																				jButton1)
																		.add(
																				18,
																				18,
																				18)
																		.add(
																				jButton2)))
										.addContainerGap(
												org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(
								org.jdesktop.layout.GroupLayout.LEADING)
						.add(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel1)
														.add(
																jTextField1,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												org.jdesktop.layout.LayoutStyle.RELATED)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jLabel2)
														.add(
																jTextField2,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE,
																org.jdesktop.layout.GroupLayout.DEFAULT_SIZE,
																org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
										.add(18, 18, 18)
										.add(
												layout
														.createParallelGroup(
																org.jdesktop.layout.GroupLayout.BASELINE)
														.add(jButton1).add(
																jButton2))
										.addContainerGap(187, Short.MAX_VALUE)));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		ProductService service = (ProductService) ContextUtils.getContext()
				.getBean("productServiceProxy");
		service.deleteCategory(category);
		this.openQueryFrame();
	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		ProductService service = (ProductService) ContextUtils.getContext()
				.getBean("productServiceProxy");
		category.setShortcode(jTextField1.getText());
		category.setSpec(jTextField2.getText());
		service.updateCategory(category);
		this.openQueryFrame();
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton jButton1;
	private javax.swing.JButton jButton2;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField jTextField1;
	private javax.swing.JTextField jTextField2;
	// End of variables declaration//GEN-END:variables

	private Logger log = LogHelper.getLog(getClass());
	private ProductCategory category;

	private void checkPermission() {
		jButton1.setVisible(false);
		jButton2.setVisible(false);
		User user = ((Login) ContextUtils.getContext().getBean("login"))
				.getUser();
		Iterator iterator = user.getFunctions().iterator();
		while (iterator.hasNext()) {
			Function function = (Function) iterator.next();
			if (PermissionConstant.MODIFY_PRO_CATEGORY_BTN.equals(function
					.getId())) {
				if (log.isDebugEnabled()) {
					log.debug(new StringBuilder("set modify button visible"));
				}
				jButton1.setVisible(true);
			}
			if (PermissionConstant.DELETE_PRO_CATEGORY_BTN.equals(function
					.getId())) {
				if (log.isDebugEnabled()) {
					log.debug(new StringBuilder("set delete button visible"));
				}
				jButton2.setVisible(true);
			}
		}
	}

	public void setCategory(ProductCategory category) {
		this.category = category;
		jTextField1.setText(category.getShortcode());
		jTextField2.setText(category.getSpec());
	}

	private void openQueryFrame() {
		JInternalFrame frame = (JInternalFrame) ContextUtils.getContext()
				.getBean("queryProductCategoryFrame");
		frame.setVisible(true);
		((JDesktopPane) this.getParent()).add(frame, 0);
		try {
			frame.setSelected(true);
			frame.setMaximum(true);
		} catch (PropertyVetoException e1) {
			log.error(new StringBuilder("set frame to selected error"), e1);
		}
		this.dispose();
	}
}